<template>
  <div class="app-container">
    <!-- 输入表单 -->
    <el-form label-width="120px">
      <el-form-item label="推荐位名称">
        <el-input v-model="adType.title" />
      </el-form-item>

      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate()">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import adTypeApi from "@/api/adType";
export default {
  name: "form",
  data(){
    return {
      saveBtnDisabled: false,
      adType:{}
    }
  },
  created() {
    if (this.$route.params.id) {
      this.fetchDataById(this.$route.params.id);
    }
  },
  methods: {
    fetchDataById(id) {
      adTypeApi.getById(id).then(res => {
        this.adType = res.data.item
      })
    },
    saveOrUpdate() {
      if (!this.adType.id) {
        adTypeApi.save(this.adType).then(response => {
          this.$message.success(response.message)
          //路由进行跳转
          this.$router.push({ path: '/ad/type-list' })
        })
      }else {
        adTypeApi.updateById(this.adType).then(response => {
          this.$message.success(response.message)
          this.$router.push({ path: '/ad/type-list' })
        })
      }
    }
  }

}
</script>

<style scoped>

</style>
